home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TCOMMPRO.TXT / XMODEM2.C < prev   
Text File  |  1987-01-14  |  8KB  |  439 lines

  1.  
  2. /*
  3.  
  4.     xmodem2.c
  5.  
  6.  
  7.     8/1/85
  8.  
  9.  
  10.     A version of Ward Christensen's MODEM program modified
  11.     by Larry Jordan Associates to include XMODEM features
  12.     and to provide compatibility with TCOMM.
  13.  
  14.     This file contains the Xmodem support functions. Functions
  15.     that are part of the The Greenleaf Functions commercial
  16.     libraries are marked in the FUNCTION.H file. You must
  17.     buy the Greenleaf Comm Library and General Library to
  18.     get the source and object code for these functions.
  19.  
  20.     Copyright (c) 1984, 1985 Larry Jordan Associates
  21. */
  22.  
  23. #include    <stdio.h>    /* Lattice header */
  24. #include    <gleafs.h>    /* Greenleaf header */
  25. #include    <timedate.h>    /* Greenleaf header */
  26. #include    <asiports.h>    /* Greenleaf header */
  27. #include    <function.h>    /* Xmodem header provided on disk */
  28. #include    <ibmpcio.h>    /* Xmodem header provided on disk */
  29. #include    <protocol.h>    /* Xmodem header provided on disk */
  30.  
  31. /*  makebuff
  32.  *
  33.  *   communications buffer initialization
  34.  *     Initialize comm port
  35.  *
  36.  *
  37.  *  Returns: None
  38.  *
  39.  */
  40.  
  41. int makebuff()
  42. {
  43.     extern int port,bps,parity,stopbits,wordlen;
  44.     extern char recvbuff[],txbuff[];
  45.  
  46.     int status,stat,n;
  47.  
  48.     stat = asisetup(port, ASINOUT, BINARYNONE, recvbuff, 500, txbuff, 500 ); /* set up interrupt buffers */
  49.     if ( stat == -1 ) return(ERROR);
  50.     asdtr(port,ON);             /* be sure DTR is on */
  51.     asrts(port,ON);             /* be sure RTS is on */
  52.  
  53.     stat = asiinit( port, bps, parity, stopbits, wordlen );   /* initialize port */
  54.     if ( stat == -1 ) return(ERROR);
  55.     stat = asistart( port, ASINOUT );   /* start IN & OUT buffers */
  56.     if ( stat == -1 ) return(ERROR);
  57.  
  58.     asixrst(port); /* read and reset .xmcount receiver char count. */
  59.  
  60.     stat = asiclear(port, ASINOUT);  /* clear buffers to start */
  61.     if(stat != -1) return(TRUE);
  62.     asiquit(port);             /* could not start clear buffers after start, so stop them and go home */
  63.     return(ERROR);
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70. /*    miready
  71.  *
  72.  *    Routine to return TRUE if modem is ready to input
  73.  *    a byte
  74.  *
  75.  *    Returns: Serial port receive character register status
  76.  */
  77.  
  78. int miready()
  79. {
  80.     extern int port;
  81.  
  82.     if ((asi_parms[port].flags & 2)==0) return(TRUE);   /* if receiver has a character */
  83.     return(FALSE);
  84.  
  85. }
  86.  
  87.  
  88. /*
  89.  *   global exit that turns off interupts first.
  90.  *
  91.  *
  92. */
  93.  
  94. int endxm()
  95. {
  96.     extern int port,actpage,monitor;
  97.  
  98.     asiquit(port);               /* turn interrupts off */
  99.     delay(10);
  100.     curtype(BLANK,6,7);           /* turn cursor off for now */
  101.     exit(0);               /* quit normal */
  102. }
  103.  
  104.  
  105.  
  106. /*
  107.  *  T I C K S
  108.  *
  109.  *  Returns total number of clock ticks since midnight
  110.  */
  111. long int ticks()
  112. {
  113.  
  114.      struct REGS regs;            /* we need a place to put results */
  115.  
  116.      union { int s[2]; long ll;} secs ;
  117.  
  118.     regs.ax = 0;
  119.     secs.ll = 0L;
  120.     int86(0x1A,®s,®s);        /* use Lattice interrupt function */
  121.     secs.s[0] = regs.dx;            /* High count */
  122.     secs.s[1] = regs.cx;            /* Low count  */
  123.     return(secs.ll);
  124.   }
  125.  
  126. /*
  127.  *  T O T S E C
  128.  *
  129.  *  Time since midnight in seconds
  130.  */
  131. long int totsec()
  132. {
  133.  
  134.       long int ticks(), secnds;
  135.  
  136.         secnds = (10l * ticks() ) / 182;
  137.         return(secnds);
  138. }
  139.  
  140.  
  141. /*
  142.  *
  143.  *   time hundreths of seconds
  144.  *
  145.  *
  146.  *
  147.  *
  148.  */
  149.  
  150. long int tothsec()
  151.  
  152. {
  153.  
  154.     struct TIMEDATE *ptd;
  155.  
  156.     long int    timehsec;
  157.     int        tsec,thsec;
  158.  
  159.  
  160.     ptd = sgettime(7);        /* get the time from system  */
  161.  
  162.     tsec = ptd->seconds;
  163.     thsec = ptd->hsecs;
  164.  
  165.     timehsec=tsec*100+thsec;
  166.  
  167.     return(timehsec);
  168.  
  169. }
  170.  
  171. /* Copyright (c) 1984, Larry Jordan Associates    */
  172.  
  173. /*
  174.  *
  175.  *
  176.  *   delay for n hundreths of seconds
  177.  *
  178.  *
  179.  *
  180.  *
  181.  */
  182.  
  183. delay(nhsec)
  184.  
  185. int nhsec;
  186. {
  187.  
  188.     long int strtime;
  189.     long int endtime;
  190.     long int nowtime;
  191.  
  192.     strtime = tothsec();
  193.     endtime = strtime + nhsec;
  194.  
  195.     while (TRUE)
  196.         {
  197.         nowtime = tothsec();
  198.         if(nowtime < strtime)
  199.             nowtime += 6000;
  200.  
  201.         if (nowtime > endtime)
  202.             break;
  203.         }
  204.  
  205. }
  206.  
  207.  
  208. /*
  209.  *   toolong
  210.  *
  211.  *   Timeout for user input with tout in seconds
  212.  *
  213.  *   Return: FALSE = time not up
  214.  *         TRUE =  time is up
  215.  */
  216.  
  217. int toolong(tout)
  218. int tout;
  219. {
  220.  
  221.     extern long int strtout;
  222.     long int nowtime;
  223.     long int elaptime;
  224.  
  225.     long int totsec();
  226.  
  227.     nowtime = totsec();
  228.     if(nowtime < strtout)           /* how does present time compare to start time mark */
  229.         nowtime += 86400;      /* past midnight, so add 24 hr of secs */
  230.     elaptime = nowtime - strtout;
  231.     if(elaptime < tout)           /* is elapsed time greater than allowed time? */
  232.         {
  233.         return(FALSE);
  234.         }
  235.     else
  236.         {
  237.         return(TRUE);
  238.         }
  239.  
  240. }
  241.  
  242.  
  243.  
  244. /*
  245.  *  readbyte
  246.  *
  247.  *  read byte from port with timeout if nothing comes in
  248.  *
  249.  */
  250. int readbyte(timeout)
  251. int timeout;
  252. {
  253.     extern    long int strtout;
  254.     extern    char    retstr[80];
  255.     extern int port;
  256.  
  257.     int    rtrn;
  258.  
  259.     if (miready())            /* is a char in buffer? */
  260.         {
  261.         rtrn = asigetc( port ); /* get it from buffer */
  262.         rtrn &= 0x00ff;
  263.         retstr[0] = rtrn;    /* put char in a string buffer */
  264.         retstr[1] = '\0';
  265.         return(TRUE);
  266.         }
  267.  
  268.     strtout = totsec();        /* reset start time mark */
  269.  
  270.     while(TRUE)            /* while there is something there to get, get it */
  271.         {
  272.         if (miready())        /* if there is a char in buffer */
  273.             {
  274.             rtrn = asigetc( port );  /* get it into rtrn */
  275.             break;             /* and get out of loop */
  276.             }
  277.         if(toolong(timeout))    /* have we waited longer than we wanted to? */
  278.             {
  279.             prosay(19,45," - timeout");
  280.             return(TIMEOUT);
  281.             }
  282.         if (asikbhit())        /* if SYSOP signalled, send his signal back */
  283.             {
  284.             if ( getch() == ESC) return(ESC);
  285.             }
  286.         if (!carrier()) return(CDLOST);   /* if carrier was lost, tell calling function */
  287.         }
  288.     rtrn &= 0x00ff;
  289.     retstr[0] = rtrn;    /* put char into string buffer */
  290.     retstr[1] = '\0';       /* be sure to terminate all strings */
  291.     return(TRUE);
  292. }
  293.  
  294.  
  295.  
  296. /*  chrput
  297.  *
  298.  *  If not in local mode and if carrier is present,
  299.  *  output a byte through serial port
  300.  *
  301.  *  chrput()
  302.  *
  303.  *  Returns: FALSE if carrier is not present
  304.  */
  305.  
  306. int chrput(c)
  307. char c;
  308. {
  309.  
  310.     extern    int port;
  311.  
  312.     long int i;
  313.  
  314.     if(!carrier()) return(CDLOST);
  315.     while( (asiputc(port,c) == -1) )
  316.      {
  317.       if(i++ > 100000L) return(CDLOST);  /* Try it a few more times */
  318.      }
  319.  
  320. }
  321.  
  322.  
  323. /*  strput
  324.  *
  325.  *  If not in local mode and if carrier is present,
  326.  *  output a string through serial port
  327.  *
  328.  *  Returns: FALSE if carrier is not present
  329.  */
  330.  
  331. int strput(string)
  332. char *string;
  333. {
  334.     int i;
  335.     int    rtrn;
  336.     char    *sptr;
  337.  
  338.     for(sptr=string, i=0 ; i < 132 ;sptr++, i++)
  339.         {
  340.         if (chrput(*sptr) == CDLOST) return(FALSE);
  341.         if (miready())                 /* is a char in buffer? */
  342.             {
  343.             rtrn = asigetc( port );        /* get it from buffer */
  344.             rtrn &= 0x00ff;
  345.             if (rtrn == CTRLX || rtrn == NAK)
  346.                 {
  347.                 retstr[0] = rtrn;     /* put char in a string buffer */
  348.                 retstr[1] = '\0';
  349.                 return(FALSE);
  350.                 }
  351.             }
  352.         }
  353.     return(TRUE);
  354.  
  355. /*........................................................
  356.     int i;
  357.  
  358.     for (string,i=0 ; i < 132; i++, string++)
  359.         {
  360.         if(chrput(*string) == CDLOST)
  361.              return(FALSE);
  362.         }
  363. .............................................................*/
  364.  
  365. }
  366.  
  367.  
  368.  
  369. /*  eatbuf
  370.  *
  371.  *  Flush communications input buffer while carrier and receive buffer set
  372.  *
  373.  *  Returns: None
  374.  */
  375.  
  376. int eatbuf()
  377. {
  378.  
  379.     extern int port;
  380.  
  381.     while(miready()) asigetc(port);
  382. }
  383.  
  384.  
  385. /*  carrier
  386.  *
  387.  *  check for existance of modem carrier signal
  388.  *
  389.  *  carrier()
  390.  *
  391.  *  Returns: TRUE if carrier present; else returns FALSE
  392.  */
  393.  
  394. int carrier()
  395. {
  396.     int status;
  397.  
  398.     status = asistat(port);           /* what is modem status? */
  399.     if (status & 0x0080) return(TRUE);    /* check rec line sig detect */
  400.     return(FALSE);                  /* return FALSE if no carrier */
  401.  
  402. }
  403.  
  404. /*
  405.  *  prosay
  406.  *
  407.  *  full screen color display for protocol info
  408.  *
  409.  */
  410. int prosay(irow,icol,str)
  411. int irow,icol;
  412. char *str;
  413. {
  414.     extern int display;
  415.     extern int cfgcolor;
  416.  
  417.     int row,col;
  418.     char c;
  419.     unsigned uret;
  420.     char *p;
  421.  
  422.     if ( !display) return;    /* if display is off from TCOMM, don't print char */
  423.     row = irow;
  424.     col = icol;
  425.  
  426.     p=str;
  427.     while (*p)
  428.         {
  429.         curset(row,col,0);       /* first, position cursor for output */
  430.         c=*p++;
  431.         cpch(c,cfgcolor,0x00,0);   /* Green leaf fast display with color */
  432.  
  433.         uret = getcur(0);       /* where are we on local screen */
  434.         row = (uret & 0xFF00) >> 8;
  435.         col = (uret & 0xFF) + 1;
  436.         }
  437.  
  438. }
  439.